package guo.dyn;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkDyn {

    private Object target;

    public JdkDyn(Object target){
        this.target = target;
    }


    public Object getProxy(){

        Class<?> targetClass = target.getClass();

        Class<?>[] interfaces = targetClass.getInterfaces();
        ClassLoader classLoader = targetClass.getClassLoader();

        InvocationHandler invocationHandler = new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("代理类方法执行前");
                Object invoke = method.invoke(target, args);
                System.out.println("执行方法后");
                return invoke;
            }
        };
        return Proxy.newProxyInstance(classLoader,interfaces,invocationHandler);
    }
}